﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Drawing;

namespace Cyklotron
{
    class CycloClient
    {
        private int hostPort;
        private byte id;
        IPAddress ipMin, ipMax, ipFound;
        public TcpClient client;
        byte velocity, magnetic_field, electric_field;
        Graphics gfx;

        public CycloClient(int hostPort, string ip, byte id, Graphics g)
        {
            this.hostPort = hostPort;
            this.id = id;
            gfx = g;
            string[] ip_adr = ip.Split('.');

            ipFound = new IPAddress(new byte[] { byte.Parse(ip_adr[0]), byte.Parse(ip_adr[1]), byte.Parse(ip_adr[2]), byte.Parse(ip_adr[3]) });

            client = new TcpClient();
            client.Connect(new IPEndPoint(ipFound, hostPort));

            new System.Threading.Thread(rcv).Start();            
        }

        static byte[] GetBytes(string str)
        {
            byte[] bytes = new byte[str.Length * sizeof(byte)];
            System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
            return bytes;
        }

        public void sendData(byte[] data)
        {

            client.Client.Send(data);
        }

        public void decrypt(ref byte[] data)
        {
            data[0] = (byte)(data[0] - data[3]);
            data[1] = (byte)(data[1] - data[3]);
            data[2] = (byte)(data[2] + data[3]);
        }

        bool initialized = false;

        public void drawParticle()
        {
            if (velocity > 50)
            {
                velocity = 50;
            }

            for (int i = 599; i > 1; i -= velocity)
			{
                gfx.DrawEllipse(Pens.Red, i, 120 + magnetic_field, 5, 5);
                System.Threading.Thread.Sleep(15);
                gfx.Clear(Color.White);
			}

            client.Client.Send(new byte[] { id, (byte)'#' , velocity });
            
        }

        void rcv()
        {
            byte[] buf = new byte[4];
            while (true)
            {
                try
                {
                    if (client.Client.Receive(buf) > 0)
                    {
                        if (initialized)
                        {
                            decrypt(ref buf);

                            velocity = buf[0];
                            electric_field = buf[1];
                            magnetic_field = buf[2];

                            velocity += electric_field;

                            drawParticle();
                        }
                        else if (buf[0] == 13)
                        {
                            initialized = true;
                        }

                    }
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                    break;
                }                
            }
        }
    }
}
